<?php


namespace app\api\controller;


use think\Request;
use app\api\model\CustomerCategory as CustomerCategoryModel;

class CustomerCategory extends Common
{
    /**
     * 客户分类 列表
     * @param Request $request
     * @param \app\api\model\CustomerCategory $model
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function index(Request $request, CustomerCategoryModel $model)
    {
        $res = $model->getList([], $request->param('page', 1), $request->param('size', 10));
        if (!empty($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }
    
    /**
     * 新增积分规则时可选客户类型
     * @param Request $request
     * @param CustomerCategoryModel $model
     */
    public function getCanUseList(Request $request, CustomerCategoryModel $model) {
        $page = $request->param('page',1);
        $size = $request->param('size',10);
        //已选客户类型
        $used_cid = db('integral_rule')->column('cid');
        $list = db('customer_category')->whereNotIn('id', $used_cid)->page($page, $size)->paginate($size);
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * 客户分类 新增
     * @param Request $request
     * @param CustomerCategoryModel $model
     * @return \think\response\Json
     */
    public function add(Request $request, CustomerCategoryModel $model)
    {
        $data = $request->only($model->getFieldArray(false));
        $res = $model->getAdd($data);
        if (!empty($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 客户分类 修改
     * @param $id
     * @param Request $request
     * @param CustomerCategoryModel $model
     * @return \think\response\Json
     */
    public function edit($id, Request $request, CustomerCategoryModel $model)
    {
        $data = $request->only($model->getFieldArray(false));
        $res = $model->getEdit($id, $data);
        if (is_int($res)) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param CustomerCategoryModel $model
     * @return \think\response\Json
     */
    public function show($id, Request $request, CustomerCategoryModel $model)
    {
        $res = $model->getCategory($id);
        if (!is_string($res)) {
            return $this->returnJson(200, '成功', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * 客户分类 删除
     * @param $id
     * @param Request $request
     * @param CustomerCategoryModel $model
     * @return \think\response\Json
     */
    public function del($id, Request $request, CustomerCategoryModel $model)
    {
        $res = $model->getDel($id);
        if (is_int($res)) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, $res);
        }
    }
}
